<?php
/**
 * ===============================================
 * 版权所有 2019-2050 青岛铁锤科技有限公司，并保留所有权利。
 * 网站地址: http://www.huaxianling.com
 * ===============================================
 * Author: Rag Hu
 * Created Time: 2019/7/17 13:57
 * Modified Time: 2019/7/17 13:57
 * Version: 2.0
 */

namespace App\Modules\Employee\Repositories;


use App\Models\UserLike;
use App\Modules\Employee\Criterias\getNewPortfolioListCriteria;
use App\Modules\Employee\Validators\UserLikeValidator;
use Flower\Core\Repository\Eloquent\BaseRepository;
use Illuminate\Support\Facades\DB;

class UserLikeRepository extends BaseRepository
{
    public function model()
    {
        return UserLike::class;
    }

    public function validator()
    {
        return UserLikeValidator::class;
    }

    public function getNewLikePortfolioByMemberId($memberId, $pageSize = null)
    {
        $pageSize = empty($pageSize) ? 4 : $pageSize;
        return $this->pushCriteria(new getNewPortfolioListCriteria($memberId, $pageSize))->get();
    }

    //获取理发师某天新增粉丝
    public function getBarberDateFansNum($user_id, $store_id, $date)
    {
        return $this->model
            ->where('user_id', $user_id)
            ->where('store_id', $store_id)
            ->whereDate('created_at', '=', $date)
            ->get()
            ->count();
    }


    public function getFansRankByBarberId($storeId, $startDate, $endDate)
    {
        $sql = 'select m.barber_id,m.barber_name,m.username,count(p.id) as counts  from (SELECT distinct u.id AS barber_id, u.nick AS barber_name, u.username as username FROM hx_user AS u LEFT JOIN hx_user_store AS s ON u.id = s.uid	 WHERE s.sid = "' . $storeId . '") as m LEFT JOIN hx_user_like AS p ON m.barber_id = p.user_id  AND DATE_FORMAT(p.created_at, \'%Y-%m-%d\') <= "' . $startDate . '" and DATE_FORMAT(p.created_at,\'%Y-%m-%d\') >="' . $endDate . '" GROUP BY m.barber_id order by counts desc';
        return DB::select($sql);
    }


    /**
     * 用户是否关注理发师
     * @param $memberId
     * @param $userId
     * @return int
     */
    public function isFollow($memberId, $userId)
    {
        $isFollow = $this->model->where(['member_id' => $memberId, 'user_id' => $userId])->first();
        return isset($isFollow) ? 1 : 0;
    }

    public function deleteUserLike($member_id,$user_id,$store_id)
    {
        $user_like = $this
            ->model
            ->where('member_id',$member_id)
            ->where('user_id',$user_id)
            ->where('store_id',$store_id)
            ->first();
        if($user_like){
            return $user_like->delete();
        }

        return true;
    }

}
